/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package org.apache.skywalking.apm.agent.core.jvm.cpu;

import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;

/**
 * @author wusheng
 *
 * MBean 和 MXBean 可用于提供有关 Java™ 虚拟机 (JVM) 状态的信息
 *
 * MXBean 是 MBean 的广义变体。
 *
 * 因为 MXBean 是仅使用一组预定义数据类型进行构造，
 *
 * 所以 MXBean 可以更容易供应用程序（例如 JConsole）所引用和使用。
 *
 */
public class SunCpuAccessor extends CPUMetricsAccessor {
    private final OperatingSystemMXBean osMBean;

    public SunCpuAccessor(int cpuCoreNum) {
        super(cpuCoreNum);

//        获得 OperatingSystemMXBean 对象
        this.osMBean = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
        this.init();
    }

    /**
     * 通过 OperatingSystemMXBean 对象
     * 获得 JVM 进程占用 CPU 总时长
     * @return
     */
    @Override
    protected long getCpuTime() {
        return osMBean.getProcessCpuTime();
    }
}
